import utilEventBus from "@/utils/eventBus";
import { getShortKeySettingIdByKey } from "@/logics/shortcutkey";
import { chromeOnMessage } from "./chromeMessage";

export const initConfig = function () {
  // 全局注册键盘事件
  console.log("全局注册事件");
  window.addEventListener("keydown", windowKeydown);

  // 存储当前 href
  chrome.storage.local.set({ href: window.location.href }).then(() => {
    console.log("自动更新 href");
  });

  // 监听 background / content 发送的信息
  chromeOnMessage();
};

async function windowKeydown(e) {
  let keyCode = e.keyCode;
  let char = String.fromCharCode(keyCode);
  let shiftKey = e.shiftKey;
  let metaKey = e.metaKey;
  let ctrlKey = e.ctrlKey;
  let altKey = e.altKey;
  let needCombindKey = false;
  let result = "";
  if (ctrlKey) {
    if (!this.isMac) {
      result += "Ctrl + ";
    } else {
      result += "Ctrl + ";
    }
    needCombindKey = true;
  }
  if (metaKey && this.isMac) {
    result += "Command + ";
    needCombindKey = true;
  }
  if (shiftKey) {
    result += "Shift + ";
    needCombindKey = true;
  }
  if (altKey) {
    if (this.isMac) {
      result += "Alt + ";
    } else {
      result += "Alt + ";
    }
    needCombindKey = true;
  }
  switch (keyCode) {
    case 8:
      // 清空快捷键
      if (result.length == 0) {
        result = "";
        $(this).val(result).trigger("change");
        return;
      }
      break;
    case 112:
      result += "F1";
      needCombindKey = false;
      break;
    case 113:
      result += "F2";
      needCombindKey = false;
      break;
    case 114:
      result += "F3";
      needCombindKey = false;
      break;
    case 115:
      result += "F4";
      needCombindKey = false;
      break;
    // case 116:
    //     result += "F5"; needCombindKey = false; break;
    case 117:
      result += "F6";
      needCombindKey = false;
      break;
    case 118:
      result += "F7";
      needCombindKey = false;
      break;
    case 119:
      result += "F8";
      needCombindKey = false;
      break;
    case 120:
      result += "F9";
      needCombindKey = false;
      break;
    case 121:
      result += "F10";
      needCombindKey = false;
      break;
    case 122:
      result += "F11";
      needCombindKey = false;
      break;
    case 123:
      result += "F12";
      needCombindKey = false;
      break;
    default:
      // 英文数字按键必须要有搭配按键才算数
      if (needCombindKey) {
        if (ctrlKey || metaKey || altKey || shiftKey) {
          if (keyCode === 187) {
            result += "=";
            needCombindKey = false;
          }
          if (keyCode === 107) {
            result += "Plus";
            needCombindKey = false;
          } else if (keyCode === 189 || keyCode === 109) {
            result += "-";
            needCombindKey = false;
          } else if (!/[^A-Za-z0-9-=+_.]/.test(char.toUpperCase())) {
            result += char.toUpperCase();
            needCombindKey = false;
          }
        }
      }
  }
  if (result && !needCombindKey) {
    const event = await getShortKeySettingIdByKey(result);
    if (event) {
      console.log("触发", event);
      utilEventBus.$emit(event);
    }
  }
}
